home *** CD-ROM | disk | FTP | other *** search
/ IRIX Patches 1995 June / SGI IRIX Patches 1995 Jun.iso / 5.3_patches / patchSG0000154 / patchSG0000154.idb / usr / share / src / OpenGL / libwidget / simple.c.z / simple.c
Encoding:
C/C++ Source or Header  |  1995-06-12  |  4.7 KB  |  185 lines

  1. #include <stdio.h>
  2. #include <Xm/Form.h>
  3. #include <Xm/Frame.h>
  4. #include <X11/keysym.h>
  5. #include <GL/GLwMDrawA.h>
  6. #include <GL/gl.h>
  7.  
  8. /* Callbacks */
  9. static void draw_scene_callback();
  10. static void do_resize();
  11. static void init_window();
  12. static void input();
  13. static void draw_scene();
  14. static void draw_polys();
  15.  
  16. static XtAppContext app_context;
  17. static GLXContext glx_context;
  18. static String fallback_resources[] = {
  19.     "*frame*shadowType: SHADOW_IN",
  20.     "*glwidget*width: 300",
  21.     "*glwidget*height: 300",
  22.     "*glwidget*rgba: TRUE",
  23.     "*glwidget*allocateBackground: TRUE",
  24.     NULL
  25.     };
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     Arg args[20];
  32.     int n;
  33.     Widget glw, toplevel, form, frame;
  34.  
  35.  
  36.     toplevel = XtAppInitialize(&app_context, "4DgiftsGLw", 
  37.                    (XrmOptionDescList) NULL, 
  38.                    (Cardinal)0,
  39.                    (int *)&argc, 
  40.                    (String*)argv, 
  41.                    fallback_resources,
  42.                    (ArgList)NULL, 0);
  43.  
  44.     n = 0;
  45.     form = XmCreateForm(toplevel, "form", args, n);
  46.     XtManageChild(form);
  47.  
  48.     n = 0;
  49.     XtSetArg(args[n], XtNx, 30); n++;
  50.     XtSetArg(args[n], XtNy, 30); n++;
  51.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  52.     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  53.     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  54.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  55.     XtSetArg(args[n], XmNleftOffset, 30); n++;
  56.     XtSetArg(args[n], XmNtopOffset, 30); n++;
  57.     XtSetArg(args[n], XmNbottomOffset, 30); n++;
  58.     XtSetArg(args[n], XmNrightOffset, 30); n++;
  59.     frame = XmCreateFrame (form, "frame", args, n);
  60.     XtManageChild (frame);
  61.  
  62.     n = 0;
  63.     glw = GLwCreateMDrawingArea(frame, "glwidget", args, n);
  64.     XtManageChild (glw);
  65.     XtAddCallback(glw, GLwNexposeCallback, draw_scene_callback, 0);
  66.     XtAddCallback(glw, GLwNresizeCallback, do_resize, 0);
  67.     XtAddCallback(glw, GLwNginitCallback, init_window, 0);
  68.     XtAddCallback(glw, GLwNinputCallback, input, 0);
  69.  
  70.     XtRealizeWidget(toplevel);
  71.  
  72.     XtAppMainLoop(app_context);
  73.  
  74. }
  75.  
  76.  
  77. static void
  78. init_window(Widget w, XtPointer client_data, XtPointer call_data)
  79. {
  80.     Arg args[1];
  81.     XVisualInfo *vi;
  82.  
  83.     XtSetArg(args[0], GLwNvisualInfo, &vi);
  84.     XtGetValues(w, args, 1);
  85.  
  86.     glx_context = glXCreateContext(XtDisplay(w), vi, 0, GL_FALSE);
  87. }
  88.  
  89.  
  90. static void
  91. draw_scene_callback(Widget w, XtPointer client_data, XtPointer call_data)
  92. {
  93.     GLfloat val;
  94.  
  95.     GLwDrawingAreaMakeCurrent(w, glx_context);
  96.     draw_scene();
  97. }
  98.  
  99. static void
  100. do_resize(Widget w, XtPointer client_data,
  101.       GLwDrawingAreaCallbackStruct *call_data)
  102. {
  103.     glViewport(0, 0, (GLint)call_data->width-1, (GLint)call_data->height-1);
  104. }
  105.  
  106. static void
  107. draw_scene(void)
  108. {
  109.     glEnable(GL_DEPTH_TEST);
  110.     glDepthFunc(GL_LEQUAL);
  111.     glClearColor(0.0, 0.0, 0.0, 0.0);
  112.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  113.     glLoadIdentity();
  114.     gluPerspective(40.0, 1.0, 30.0, 1000.0);
  115.     glTranslatef(0.0, 0.0, -40.0);
  116.     glRotatef(-58.0, 0,1,0);
  117.     draw_polys();
  118.     glFlush();
  119. }
  120.  
  121. float polygon1[3][3] = { {-10.0, -10.0,   0.0,},
  122.                          { 10.0, -10.0,   0.0,},
  123.                          {-10.0,  10.0,   0.0,} };
  124.  
  125. float polygon2[3][3] = { {  0.0, -10.0, -10.0,},
  126.                          {  0.0, -10.0,  10.0,},
  127.                          {  0.0,   5.0, -10.0,} };
  128.  
  129. float polygon3[4][3] = { {-10.0,   6.0,   4.0,},
  130.                          {-10.0,   3.0,   4.0,},
  131.                          {  4.0,  -9.0, -10.0,},
  132.                          {  4.0,  -6.0, -10.0,} };
  133.  
  134. static void
  135. draw_polys() {
  136.     glBegin(GL_POLYGON);
  137.         glColor3f(0.0, 0.0, 0.0);
  138.         glVertex3fv(&polygon1[0][0]);
  139.         glColor3f(0.7, 0.7, 0.7);
  140.         glVertex3fv(&polygon1[1][0]);
  141.         glColor3f(1.0, 1.0, 1.0);
  142.         glVertex3fv(&polygon1[2][0]);
  143.     glEnd();
  144.     glBegin(GL_POLYGON);
  145.         glColor3f(1.0, 1.0, 0.0);
  146.     glVertex3fv(&polygon2[0][0]);
  147.     glColor3f(0.0, 1.0, 0.7);
  148.     glVertex3fv(&polygon2[1][0]);
  149.     glColor3f(0.0, 0.0, 1.0);
  150.     glVertex3fv(&polygon2[2][0]);
  151.     glEnd();
  152.     glBegin(GL_POLYGON);
  153.         glColor3f(1.0, 1.0, 0.0);
  154.     glVertex3fv(&polygon3[0][0]);
  155.     glColor3f(1.0, 0.0, 1.0);
  156.     glVertex3fv(&polygon3[1][0]);
  157.     glColor3f(0.0, 0.0, 1.0);
  158.     glVertex3fv(&polygon3[2][0]);
  159.     glColor3f(1.0, 0.0, 1.0);
  160.     glVertex3fv(&polygon3[3][0]);
  161.     glEnd();
  162. }
  163.  
  164. /* Process all Input callbacks*/
  165. static void
  166. input(Widget w, XtPointer client_data, GLwDrawingAreaCallbackStruct *call_data)
  167. {
  168.     char buffer[1];
  169.     KeySym keysym;
  170.  
  171.     switch(call_data->event->type)
  172.     {
  173.     case KeyRelease:
  174.     /* It is necessary to convert the keycode to a keysym before
  175.      * it is possible to check if it is an escape
  176.      */
  177.     if (XLookupString((XKeyEvent *) call_data->event,buffer,1,&keysym,NULL)
  178.         == 1 && keysym == (KeySym)XK_Escape)
  179.         exit(0);
  180.     break;
  181.     }
  182. }
  183.  
  184.  
  185.